/*
 * Copyright [2011] [Ilya Rudakov]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var SUCCESS = "success";
var IS_RUNNING = "is_running";
var IS_BROKEN = "is_broken";
var STATELESS = "stateless";
var WAITING_FOR_CHECK = "waiting_for_check";

var STATE_CODES_TO_CLASS = {"success": "successful_state", "is_running": "successful_state",
    "is_broken": "broken_state", "stateless": "stateless", "waiting_for_check": "waiting_for_check"};

function initFeedStateChecker(interval) {
    setInterval(check_feed_states, interval);
    check_feed_states();
}

function check_feed_states() {
    var feed_ids = get_feed_ids();
    $.getJSON("/jm/get_feed_states", feed_ids, function(feed_state_array) {
        $.each(feed_state_array, function(index, feed_state) {
            var f_control_panel = $(".feed_control_panel:has(span.feed_id_holder:contains(" + feed_state['feed_id'] + "))");

            var feed_state_code = feed_state['state'];
            set_state(f_control_panel, feed_state_code);

            var feed_pid_holder = f_control_panel.find(".feed_pid_holder");
            feed_pid_holder.text(feed_state['pid']);
        });
    });
}

function set_state(feed_control_panel, state) {
    var start_feed_button = feed_control_panel.find(".start_feed_button");
    var interrupt_feed_button = feed_control_panel.find(".interrupt_feed_button");
    var feed_run_loader_holder = feed_control_panel.find(".feed_run_loader_holder");

    feed_control_panel.removeClass(STATE_CODES_TO_CLASS[WAITING_FOR_CHECK]);
    feed_control_panel.removeClass(STATE_CODES_TO_CLASS[SUCCESS]);
    feed_control_panel.removeClass(STATE_CODES_TO_CLASS[IS_BROKEN]);
    feed_control_panel.removeClass(STATE_CODES_TO_CLASS[IS_RUNNING]);
    feed_control_panel.removeClass(STATE_CODES_TO_CLASS[STATELESS]);
    feed_control_panel.addClass(STATE_CODES_TO_CLASS[state]);

    switch (state) {
        case SUCCESS:
            feed_run_loader_holder.hide();
            start_feed_button.show();
            interrupt_feed_button.hide();
            break;
        case IS_RUNNING:
            feed_run_loader_holder.show();
            start_feed_button.hide();
            interrupt_feed_button.show();
            break;
        case IS_BROKEN:
            feed_run_loader_holder.hide();
            start_feed_button.show();
            interrupt_feed_button.hide();
            break;
        case STATELESS:
            feed_run_loader_holder.hide();
            start_feed_button.show();
            interrupt_feed_button.hide();
            break;
        case WAITING_FOR_CHECK:
            feed_run_loader_holder.hide();
            start_feed_button.hide();
            interrupt_feed_button.hide();
            break;
    }
}